#!/usr/bin/env python
# -*- coding: utf-8 -*-

'''eof
name:企业类型（引入投资人数量）
code:EG_CompanyTypeOptimize
tableName:
columnName:
groups:企业通用
dependencies:EG
type:常用指标
dataSourceType:在线指标
description:
eof'''

def CompanyTypeOptimize():
    company_type = EG['srcData'][0]['EG']['results'][0]["jbxx"]["company_type"]
    companycompany_type = EG['srcData'][0]['EG']['results'][0]["jbxx"]['company_companytype']

    convertedType = convertCompanyType(company_type, companycompany_type)
    shareholder_amount = ShareholderNum()
    if shareholder_amount == u'缺失值':
        shareholder_amount = 0
    if convertedType != u"有限责任公司":
        return convertedType
    elif convertedType == u"有限责任公司" and shareholder_amount >= 3:
        return u"有限责任公司(投资人>=3)"
    else:
        return u"有限责任公司(投资人<3)"


def convertCompanyType(type, companycompany_type):
    try:
        # if type == None:
        #     return u'缺失值'
        bool1 = (u"分" in type) or (u"经营单位" in type) or (u"办事处" in type) or (u"办事机构" in type) or (u"机构" in type)
        bool2 = (type == u"外国（地区）企业在中国境内从事经营活动") or (type == u"外国（地区）企业（其他）")
        if bool1 or bool2:
            return u"分支机构等"

        bool3 = (u"外" in type) and (u"独资" in type) and (u"投资" not in type)
        bool4 = (u"港" in type) and (u"独资" in type)
        bool5 = (u"澳" in type) and (u"独资" in type)
        bool6 = (u"台" in type) and (u"独资" in type)
        bool7 = (u"有限责任公司（台港澳一人）" in type)
        if bool3 or bool4 or bool5 or bool6 or bool7:
            return u"外商独资"

        if type == u"个人独资企业":
            return type

        if u"个" in type:
            return u"个体工商户"

        if u"股份有限公司" in type:
            return u"股份有限公司"

        if (u"有限责任公司（非自然人投资或控股的法人独资）" in type) or (u"有限责任公司（国有独资）" in type):
            return u"有限责任公司"

        if ((u"有限责任公司" in type) and (u"独资" in type)) or (type == u"一人有限责任公司") or (type == u"有限公司（自然人独资）"):
            return u"一人有限责任公司"

        if u"有限责任公司" in type:
            return u"有限责任公司"

        if u"合伙" in type:
            return u"合伙企业"

        if (u"企业" in type) or (type == u"内资公司") or (type == u"公司") or (type == u"非公司") or (type == u"集团"):
            return u"其他公司类型"

        if companycompany_type not in [None, '', 'null', 'NULL', 'Null']:
            return u"其他"

        return u'缺失值'
    except:
        return u'缺失值'

def ShareholderNum():
    try:
        return len(EG['content']['srcData'][0]['EG']['results'][0]["gdxx"])
    except:
        return u'缺失值'

result = CompanyTypeOptimize()